// Copyright 2019 Google LLC.
#ifndef FontCollection_DEFINED
#define FontCollection_DEFINED

#include <cstdint>
#include <memory>
#include <native_drawing/drawing_text_declaration.h>
#include <optional>
#include <set>
#include "include/core/SkFontMgr.h"
#include "include/core/SkRefCnt.h"
#include "modules/skparagraph/include/FontArguments.h"
#include "modules/skparagraph/include/ParagraphCache.h"
#include "modules/skparagraph/include/TextStyle.h"
#include "src/ports/SkFontMgr_ohos.h"
#include "src/core/SkTHash.h"

#include <native_drawing/drawing_font_collection.h>
#include <native_drawing/drawing_register_font.h>
#include <sys/types.h>
namespace skia {
namespace textlayout {

class TextStyle;
class Paragraph;
class SK_API FontCollection : public SkRefCnt {
public:
    FontCollection();
    ~FontCollection();
    
    FontCollection(const FontCollection&) = delete;
    FontCollection& operator=(const FontCollection&) = delete;
    
    FontCollection(FontCollection&&) = delete;
    FontCollection& operator=(FontCollection&&) = delete;

//    bool isSystemThemeFont() {
//        return fEnableSystemThemeFont;
//    }
    // 0: success
    // 1: file not found or para error
    // 2: file open failed
    // 3: read file failed
    // 4: find file failed
    // 5: get file size failed
    // 9: file broken
//    uint32_t registerFont(const SkString* fontName, const SkString* fontFilePath) {
//        if (!fontName || !fontFilePath) {
//            return 1;
//        }
//        auto ret = OH_Drawing_RegisterFont(getFontCollectionOHOS().get(), fontName->c_str(), fontFilePath->c_str());
//        if (ret == 0) {
//            fRegisteredFontNames.push_back(*fontName);
//        }
//        return ret;
//    }
//    const std::vector<SkString>& getRegisteredFontNames() { return fRegisteredFontNames; }
    size_t getFontManagersCount() const;

    void setAssetFontManager(sk_sp<SkFontMgr> fontManager);
    void setDynamicFontManager(sk_sp<SkFontMgr> fontManager);
    void setTestFontManager(sk_sp<SkFontMgr> fontManager);
    void setDefaultFontManager(sk_sp<SkFontMgr> fontManager);
    void setDefaultFontManager(sk_sp<SkFontMgr> fontManager, const char defaultFamilyName[]);
    void setDefaultFontManager(sk_sp<SkFontMgr> fontManager, const std::vector<SkString>& defaultFamilyNames);

    sk_sp<SkFontMgr> getFallbackManager() const { return fDefaultFontManager; }

    std::vector<sk_sp<SkTypeface>> findTypefaces(const std::vector<SkString>& familyNames, SkFontStyle fontStyle);
    std::vector<sk_sp<SkTypeface>> findTypefaces(const std::vector<SkString>& familyNames, SkFontStyle fontStyle, const std::optional<FontArguments>& fontArgs);

    sk_sp<SkTypeface> defaultFallback(SkUnichar unicode, SkFontStyle fontStyle, const SkString& locale);
    sk_sp<SkTypeface> defaultFallback();

    void disableFontFallback();
    void enableFontFallback();
    bool fontFallbackEnabled() { return fEnableFontFallback; }

    ParagraphCache* getParagraphCache() { return &fParagraphCache; }

    void clearCaches();

    static std::shared_ptr<OH_Drawing_FontCollection> getFontCollectionOHOS() {
        static SkOnce once;
        static std::shared_ptr<OH_Drawing_FontCollection> fFontCollectionOHOS;
        once([] {
            fFontCollectionOHOS = std::shared_ptr<OH_Drawing_FontCollection>(
                OHDrawingAPI::OH_Drawing_GetFontCollectionGlobalInstance(),
                OHFontCollectionDeleter
            );
        });
        return fFontCollectionOHOS;
    }

    static uint32_t getCustomFontNumAndInc() { 
        return fCustomFontNumFromMakeFromData.fetch_add(1, std::memory_order_relaxed);
    }

    static SkString getNextFontName() {
        uint32_t nameIndex = skia::textlayout::FontCollection::getCustomFontNumAndInc();
        return SkString("HarmonyOS_Custom_Font_" + std::to_string(nameIndex));
    }

    operator OH_Drawing_FontCollection*() {
        return getFontCollectionOHOS().get();
    }
    
    uint64_t hash() {
        DrawingHash hasher;
        hasher.push(fEnableFontFallback);
        hasher.push(fDefaultFontManager.get());
        hasher.push(fAssetFontManager.get());
        hasher.push(fDynamicFontManager.get());
        hasher.push(fTestFontManager.get());
        for (auto& s: fDefaultFamilyNames) {
            hasher.pushArray(s.c_str(), s.size());
        }
        return hasher.digest();
    }

private:
    std::vector<sk_sp<SkFontMgr>> getFontManagerOrder() const;

    sk_sp<SkTypeface> matchTypeface(const SkString& familyName, SkFontStyle fontStyle);

    static void OHFontCollectionDeleter(OH_Drawing_FontCollection* collection) {
        if (collection && !OHDrawingAPI::support_OH_Drawing_GetFontCollectionGlobalInstance) {
            OH_Drawing_DestroyFontCollection(collection);
        }
    }

    struct FamilyKey {
        FamilyKey(const std::vector<SkString>& familyNames, SkFontStyle style, const std::optional<FontArguments>& args)
                : fFamilyNames(familyNames), fFontStyle(style), fFontArguments(args) {}

        FamilyKey() {}

        std::vector<SkString> fFamilyNames;
        SkFontStyle fFontStyle;
        std::optional<FontArguments> fFontArguments;

        bool operator==(const FamilyKey& other) const;

        struct Hasher {
            size_t operator()(const FamilyKey& key) const;
        };
    };

    bool fEnableFontFallback;
//    bool fEnableSystemThemeFont = false;
    skia_private::THashMap<FamilyKey, std::vector<sk_sp<SkTypeface>>, FamilyKey::Hasher> fTypefaces;
    sk_sp<SkFontMgr> fDefaultFontManager;
    sk_sp<SkFontMgr> fAssetFontManager;
    sk_sp<SkFontMgr> fDynamicFontManager;
    sk_sp<SkFontMgr> fTestFontManager;
//    OH_Drawing_FontCollection* fFontCollectionOHOS = nullptr;
//    static std::shared_ptr<OH_Drawing_FontCollection> fFontCollectionOHOS;
    static std::atomic<uint32_t> fCustomFontNumFromMakeFromData;

    std::vector<SkString> fDefaultFamilyNames;
    ParagraphCache fParagraphCache;
};
}  // namespace textlayout
}  // namespace skia

#endif  // FontCollection_DEFINED
